﻿using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel.Composition;

namespace Steve.UI
{
    public class ContentDataHandlerEditData
    {
        public MimeType MimeType { get; set; }
        public string MimeSubtype { get; set; }
        public Stream Data { get; set; }
        public int? Height { get; set; }
        public int? Width { get; set; }
    }

    //TODO: split the UI from non UI stuff?
    public abstract class ContentDataHandlerBase
    {
        [Import]
        public ContentAdministrationRepository ContentUpdateRepository { get; set; }

        public abstract string ExtractText(Content content, ContentType contentType);
        public abstract ActionResult Render(Content content, ContentType contentType, bool raw);
        public abstract ActionResult Edit(Content content, ContentType contentType);
        public void SaveEditData(ContentType contentType, Content toUpdate)
        {
            if (toUpdate == null) throw new ArgumentNullException("toUpdate");

            var data = GetEditData(contentType);

            if (data == null) return;

            using (var s = data.Data)
            {
                toUpdate.ContentDataId = ContentUpdateRepository.NewContentData(s);
            }

            toUpdate.MimeType = data.MimeType;
            toUpdate.MimeSubtype = data.MimeSubtype;
            toUpdate.Height = data.Height;
            toUpdate.Width = data.Width;
        }

        protected abstract ContentDataHandlerEditData GetEditData(ContentType contentType);
    }
}
